package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.DicContent;
import com.softer.wxzj.mapper.BaseBuildInfoMapper;
import com.softer.wxzj.mapper.BaseHouseInfoMapper;
import com.softer.wxzj.mapper.BaseModifyDetailMapper;
import com.softer.wxzj.mapper.DicContentMapper;
import com.softer.wxzj.service.DicContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class DicContentServiceImpl implements DicContentService {
    @Autowired
    private DicContentMapper diccontentMapper;
    @Autowired
    private BaseBuildInfoMapper baseBuildInfoMapper;
    @Autowired
    private BaseHouseInfoMapper baseHouseInfoMapper;
    @Autowired
    private BaseModifyDetailMapper baseModifyDetailMapper;
    @Value("${registerType}")
    private String registerType;

    @Override
    public CommonResult insert(DicContent diccontent) {
        diccontent.setCreater(ShiroUserUtil.getUserName());
        int i = diccontentMapper.insert(diccontent);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(DicContent diccontent) {
        diccontent.setCreater(ShiroUserUtil.getUserName());
        int i = diccontentMapper.update(diccontent);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult delete(String id) {
        int num = baseBuildInfoMapper.selectType(id);
        if (num > 0) {
            return CommonResult.ResponseResult(0, "已被使用无法删除");
        }
        num = baseHouseInfoMapper.selectType(id);
        if (num > 0) {
            return CommonResult.ResponseResult(0, "已被使用无法删除");
        }
        num = baseModifyDetailMapper.selectZoneCode(id);
        if (num > 0) {
            return CommonResult.ResponseResult(0, "已被使用无法删除");
        }
        int i = diccontentMapper.delete(id);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public PageInfo<DicContent> getList(Page page, DicContent diccontent) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DicContent> list = diccontentMapper.getList(diccontent);
        PageInfo<DicContent> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public DicContent getInfo(String id) {
        return diccontentMapper.getInfo(id);
    }

    @Override
    public PageInfo<DicContent> getListbyDicCode(Page page,DicContent dic) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DicContent> list= diccontentMapper.selectListByDicCode(dic);
        PageInfo<DicContent> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<DicContent> selectForInput(String dicType) {
        return diccontentMapper.selectForInput(dicType);
    }

    @Override
    public PageInfo<DicContent> fuzzySearch(Page page, String fuzzy, String typeId) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DicContent> list = diccontentMapper.fuzzySearch(fuzzy, typeId);
        PageInfo<DicContent> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据字典类型查询字典详情
     *
     * @param dicType 字典类型
     * @return 字典详情
     */
    @Override
    public List<DicContent> getListByDicType(String dicType) {
        return diccontentMapper.getListByDicType(dicType);
    }

    /**
     * 查询外网注册类型列表
     *
     * @return 外网注册类型列表
     */
    @Override
    public List<DicContent> getRegisterType() {
        return diccontentMapper.getRegisterType(registerType);
    }
}
